package com.secsbrain.development.biz.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.secsbrain.development.biz.IDeployPlanTestService;
import com.secsbrain.development.biz.IProjectAppService;
import com.secsbrain.development.biz.constant.EnumDefinition;
import com.secsbrain.development.dal.mapper.ProjectAppMapper;
import com.secsbrain.development.dal.model.DeployPlanTest;
import com.secsbrain.development.dal.model.ProjectApp;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.secsbrain.common.ErrorCodeEnum;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.development.client.model.TestDetail;
import com.secsbrain.development.client.model.TestResult;
import com.baomidou.framework.service.impl.SuperServiceImpl;

/**
 *
 * ProjectApp 表数据服务层接口实现类
 *
 */
@Service
public class ProjectAppServiceImpl extends SuperServiceImpl<ProjectAppMapper, ProjectApp> implements IProjectAppService {
	
	@Autowired
    IDeployPlanTestService deployPlanTestService;

	/* (non-Javadoc)
	 * @see IProjectAppService#getTestResult(java.lang.Long)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseVo<TestResult> getTestResult(Long appId,Long projectId) {
		DeployPlanTest deployPlanTestRes=null;

		if(appId!=null){
			ProjectApp projectAppParam=new ProjectApp();
			projectAppParam.setAppId(appId);
			List<ProjectApp> projectAppList=this.selectList(projectAppParam," created_time desc");
			if(CollectionUtils.isEmpty(projectAppList)){
				//不在发布计划内直接返回成功
				return ResponseVo.getSuccessResponse();
			}
			
			for(ProjectApp projectApp:projectAppList){
				DeployPlanTest deployPlanTestParam=new DeployPlanTest();
				deployPlanTestParam.setPlanId(projectApp.getProjectId());
				DeployPlanTest deployPlanTest=deployPlanTestService.selectOne(deployPlanTestParam);
				if(deployPlanTest!=null){
					deployPlanTestRes=deployPlanTest;
				}
				if(deployPlanTest!=null&& EnumDefinition.TestStatus.FINISH.getCode().equals(deployPlanTest.getStatus())){
					deployPlanTestRes=deployPlanTest;
					break;
				}
			}
		}else{
			Assert.notNull(projectId, "projectId不能为空");
			DeployPlanTest deployPlanTestParam=new DeployPlanTest();
			deployPlanTestParam.setPlanId(projectId);
			deployPlanTestRes=deployPlanTestService.selectOne(deployPlanTestParam);
		}
		
		if(deployPlanTestRes==null){
			return ErrorCodeEnum.ERROR_COMMON_CHECK.getResponseVo("此应用没有在SIT环境发布过");
		}
		
		if(!EnumDefinition.TestStatus.FINISH.getCode().equals(deployPlanTestRes.getStatus())){
			if(EnumDefinition.TestStatus.FAIL.getCode().equals(deployPlanTestRes.getStatus())){
				return ErrorCodeEnum.ERROR_COMMON_CHECK.getResponseVo("自动化测试没有请求成功");
			}
			if(EnumDefinition.TestStatus.ING.getCode().equals(deployPlanTestRes.getStatus())){
				return ErrorCodeEnum.ERROR_COMMON_CHECK.getResponseVo("自动化测试正在执行，还没返回结果");
			}
			if(EnumDefinition.TestStatus.INIT.getCode().equals(deployPlanTestRes.getStatus())){
				return ErrorCodeEnum.ERROR_COMMON_CHECK.getResponseVo("自动化测试还没有执行");
			}
			if(EnumDefinition.TestStatus.NONE.getCode().equals(deployPlanTestRes.getStatus())){
				return ErrorCodeEnum.ERROR_COMMON_CHECK.getResponseVo("SIT有应用发布失败");
			}
		}
		
		TestResult testResult=new TestResult();
		testResult.setCode(deployPlanTestRes.getTestCode());
		testResult.setDetailList(JSON.parseArray(deployPlanTestRes.getDetail(), TestDetail.class));
		return ResponseVo.getSuccessResponse(testResult);
	}


}